package com.inspire;

import com.process.ZoaThreadLocal;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

public class Req2 {
    public static void main(String[] args) {
        // 目标 URL
        String url = "http://localhost:8080/TomcatWebProjJdk8-1.0-SNAPSHOT/hello-servlet";
        ZoaThreadLocal.G_Ins().A_CInfByID("gyt");

        // 创建 HttpClient 实例
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 创建 POST 请求
            HttpPost httpPost = new HttpPost(url);

            // 设置请求头
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

            // 设置表单数据
            List<NameValuePair> formData = new ArrayList<>();
            formData.add(new BasicNameValuePair("key1", "value1"));
            formData.add(new BasicNameValuePair("key2", "value2"));
            httpPost.setEntity(new UrlEncodedFormEntity(formData));

            // 发送请求并获取响应
            HttpResponse response = httpClient.execute(httpPost);

            // 获取响应状态码
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("Response Status Code: " + statusCode);

            // 获取响应体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                String responseBody = EntityUtils.toString(responseEntity);
                System.out.println("Response Body: " + responseBody);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
